package com.mindcoord.handbook.controller;

import com.alibaba.fastjson.JSON;
import com.mindcoord.handbook.common.Result;
import com.mindcoord.handbook.pojo.Student;
import com.mindcoord.handbook.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/student", produces = "application/json; charset=utf-8")
public class StudentController {

    private StudentService studentService;

    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @GetMapping("/")
    public Object listStudents() {
        Result result = studentService.listStudents();
        return JSON.toJSONString(result);
    }

    @GetMapping("/list/{clazzId}")
    public Object listStudentsByClazzId(@PathVariable("clazzId") Integer clazzId) {
        Result result = studentService.listStudentsByClazzId(clazzId);
        return JSON.toJSONString(result);
    }

    @GetMapping("/{id}")
    public Object getStudentById(@PathVariable("id") Integer id) {
        Result result = studentService.getStudentById(id);
        return JSON.toJSONString(result);
    }

    @PostMapping("/put")
    public Object insertStudent(Student student) {
        Result result = studentService.insertStudent(student);
        return JSON.toJSONString(result);
    }

    @DeleteMapping("/{id}")
    public Object deleteStudentById(@PathVariable("id") Integer id) {
        Result result = studentService.deleteStudentById(id);
        return JSON.toJSONString(result);
    }

    @PutMapping("/put")
    public Object updateStudent(Student student) {
        Result result = studentService.updateStudent(student);
        return JSON.toJSONString(result);
    }

}
